/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "los_swtmr.h"
#include "time.h"
#include "los_sys.h"
#include "los_api_timer.h"
#include "los_inspect_entry.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_timercount3 = 0;
static UINT32 g_timercount2 = 0;
static UINT32 g_timercount1 = 0;
static UINT32 g_Tickcount2[20] = {0};
static UINT32 g_Tickcount3[20] = {0};
static UINT32 g_Tickcount4[20] = {0};
static VOID Timer1_Callback(UINT32 arg)
{
    UINT32 uwRet = LOS_OK;
    // dprintf("\r\n--------Timer1_Callback--------\r\n");
    g_Tickcount2[g_timercount1] = (UINT32)LOS_TickCountGet();
    g_timercount1++;
}
static VOID Timer2_Callback(UINT32 arg)
{
    UINT32 uwRet = LOS_OK;
    // dprintf("\r\n--------Timer2_Callback--------\r\n");
    g_Tickcount3[g_timercount2] = (UINT32)LOS_TickCountGet();
    g_timercount2++;
}
static VOID Timer3_Callback(UINT32 arg)
{
    UINT32 uwRet = LOS_OK;
    // dprintf("\r\n--------Timer2_Callback--------\r\n");
    g_Tickcount4[g_timercount3] = (UINT32)LOS_TickCountGet();
    g_timercount3++;
}
UINT32 Example_TimerAlign2Entry(VOID)
{
    UINT16 id1;// timer id
    UINT16 id2;// timer id
    UINT16 id3;// timer id
    UINT32 uwRet = LOS_OK;
    UINT8 i;// timer id
    UINT32 Temp_data[3];
    dprintf("\r\n--------4.11 Timer align2 test!--------\r\n");
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer1_Callback, &id1, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer1_Callback, &id1, 1);
#endif
    if(LOS_OK != uwRet)
    {
        dprintf("create Timer1 failed .\r\n");
    }
    else
    {
        dprintf("\r\n1. create Timer1 success! uwInterval is 500 ms.\r\n");
    }
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
    uwRet = LOS_SwtmrCreate(1500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    uwRet = LOS_SwtmrCreate(1500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1);
#endif
    if(LOS_OK != uwRet)
    {
        dprintf("create Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n1. create Timer2 success! uwInterval is 1500 ms.\r\n");
    }
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
    uwRet = LOS_SwtmrCreate(4500, LOS_SWTMR_MODE_PERIOD, Timer3_Callback, &id3, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    uwRet = LOS_SwtmrCreate(4500, LOS_SWTMR_MODE_PERIOD, Timer3_Callback, &id3, 1);
#endif
    if(LOS_OK != uwRet)
    {
        dprintf("create Timer3 failed .\r\n");
    }
    else
    {
        dprintf("\r\n1. create Timer3 success! uwInterval is 4500 ms.\r\n");
    }
    Temp_data[0] = (UINT32)LOS_TickCountGet();
    LOS_SwtmrStart(id3);
    (VOID)LOS_TaskDelay(7);
    Temp_data[1] = (UINT32)LOS_TickCountGet();
    LOS_SwtmrStart(id2);
    (VOID)LOS_TaskDelay(8);
    Temp_data[2] = (UINT32)LOS_TickCountGet();
    LOS_SwtmrStart(id1);
    dprintf("\r\n4. LOS_SwtmrStart(id3) is %d .", Temp_data[0]);
    dprintf("\r\n4. LOS_SwtmrStart(id2) is %d .", Temp_data[1]);
    dprintf("\r\n4. LOS_SwtmrStart(id1) is %d .", Temp_data[2]);
    dprintf("\r\n3. LOS_TaskDelay 9500ms!\r\n");
    (VOID)LOS_TaskDelay(9500);

    LOS_SwtmrStop(id1);
    LOS_SwtmrStop(id2);
    LOS_SwtmrStop(id3);


    LOS_SwtmrDelete(id1);
    LOS_SwtmrDelete(id2);
    LOS_SwtmrDelete(id3);

    while(g_timercount3<2)
    {
        LOS_TaskDelay(100);
    }
    dprintf("\r\n3. Alignment time is %d:",g_Tickcount2[0]-(g_Tickcount2[1]-g_Tickcount2[0]));
    for(i=0;i<g_timercount1;i++)
    {
        dprintf("\r\n4. Timer1_Callback entry num=%d, g_timercount1=%d .", i, g_Tickcount2[i]);
    }
    dprintf("\r\n3. Alignment time is %d:",g_Tickcount3[0]-(g_Tickcount3[1]-g_Tickcount3[0]));
    for(i=0;i<g_timercount2;i++)
    {
        dprintf("\r\n4. Timer2_Callback entry num=%d, g_timercount2=%d .", i, g_Tickcount3[i]);
    }
    dprintf("\r\n3. Alignment time is %d:",g_Tickcount4[0]-(g_Tickcount4[1]-g_Tickcount4[0]));
    for(i=0;i<g_timercount3;i++)
    {
        dprintf("\r\n4. Timer3_Callback entry num=%d, g_timercount3=%d .", i, g_Tickcount4[i]);
    }
    dprintf("\r\n");
    g_timercount3=0;
    g_timercount2=0;
    g_timercount1=0;
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
